#include <iostream>
#include <vector>

using namespace std;
class Solution
{
public:
    string reverseOnlyLetters(string s)
    {
        string ans;
        for (char c : s)
        {
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
            {
                ans += c;
            }
        }
        cout << ans << endl;
        string ret;
        // "Test1ng-Leet=code-Q!"
        for (int i = 0, j = ans.length() - 1; i < s.length() || j >= 0; i++)
        {
            int c = s[i];
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
            {
                ret += ans[j];
                j--;
            }
            else
            {
                ret += s[i];
            }
        }
        return ret;
    }

    string reverseOnlyLetters_2(string s)
    {
        if (s.empty())
        {
            return s;
        }
        auto isLetter = [=](char ch) -> bool
        {
            if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
            {
                return true;
            }
            return false;
        };
        size_t begin = 0, end = s.size() - 1;
        while (begin < end)
        {
            while (begin < end && !isLetter(s[begin]))
            {
                begin++;
            }
            while (begin < end && !isLetter(s[end]))
            {
                end--;
            }
            swap(s[begin], s[end]);
            begin++;
            end--;
        }
        return s;
    }
};
int main()
{
    Solution s;
    string str = "a-bC-dEf-ghIj";
    cout << s.reverseOnlyLetters(str) << endl;
    return 0;
}